package app.util;

import java.io.File;
import java.io.FileNotFoundException;

import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.SecureRequestCustomizer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.SslConnectionFactory;
import org.eclipse.jetty.util.ssl.SslContextFactory;

import app.handler.HelloHandler;

/**
 * 
 * @author liuZhenghua
 * @date 2015年8月6日 下午1:40:45
 * @version 1.0
 * @since
 * 生成key：keytool -genkey -alias hbpu -keypass 123456 -keyalg RSA -keysize 1024 -validity 365 -keystore d:\hbpu.keystore -storepass 123456
 * 生成key
 * 查看key：keytool -list -v -keystore d:\hbpu.keystore -storepass 123456
 * 证书导出：keytool -export -alias hbpu -keystore d:\hbpu.keystore -file d:\hbpu.crt -storepass 123456
 * 查看证书：keytool -printcert -file d:\hbpu.crt
 * 证书条目删除：keytool -delete -alias hbpu -keystore d:\hbpu.keystore -storepass 123456
 * 证书导入到keystore：keytool -import -alias hbpu -file d:\hbpu.crt -keystore d:\hbpu.keystore -storepass 123456
 */



/*<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-security</artifactId>
<version>${project.version}</version>
</dependency> 
*/


public class ManyConnectors {
	
	public static void main(String[] args) throws Exception {
		//String keyStore = "keystore";
		String keyStore = "hbpu.keystore";
		File keyStoreFile = new File("src/main/resources/" + keyStore);
		if (!keyStoreFile.exists()) {
			throw new FileNotFoundException(keyStoreFile.getAbsolutePath());
		}
		
		Server server = new Server();
		HttpConfiguration http_config = new HttpConfiguration();
		http_config.setSecureScheme("https");
		http_config.setSecurePort(8443);
		http_config.setOutputBufferSize(32768);
		
		ServerConnector http = new ServerConnector(server,
				new HttpConnectionFactory(http_config));
		http.setPort(8082);
		http.setIdleTimeout(30000);
		
		SslContextFactory sslContextFactory = new SslContextFactory();
		sslContextFactory.setKeyStorePath(keyStoreFile.getAbsolutePath());
		//sslContextFactory.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
		//sslContextFactory.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
		sslContextFactory.setKeyStorePassword("123456");
		sslContextFactory.setKeyManagerPassword("123456");
		
		HttpConfiguration https_config = new HttpConfiguration(http_config);
		https_config.addCustomizer(new SecureRequestCustomizer());
		
		ServerConnector https = new ServerConnector(server,
				new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
				new HttpConnectionFactory(https_config));
		https.setPort(8443);
		https.setIdleTimeout(500000);
		
		server.setConnectors(new Connector[] {http, https});
		server.setHandler(new HelloHandler());
		
		server.start();
		server.join();
	}
}
